home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / AppWannabe / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  19.8 KB  |  591 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Window.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the main Wannabe
  20. ** document.  Wannabe currently only supports one type of documents, type 'DUMD',
  21. ** which stands for "DUMb Document". */
  22.  
  23. /* For more information on this file, please read the read.me file "=How to write your app". */ 
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for application.            */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __FONTS__
  40. #include <Fonts.h>
  41. #endif
  42.  
  43. #ifndef __RESOURCES__
  44. #include <Resources.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51. #ifndef __UTILITIES__
  52. #include "Utilities.h"
  53. #endif
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58.  
  59.  
  60.  
  61. Boolean        gNoDefaultDocument = false;
  62.                     /* Set to true if app should boot with no default document. */
  63.                     /* This tells DTS.Lib..framework what you want. */
  64.  
  65. OSType        gAppWindowType = kDocFileType;    /* Main document type. */
  66. long        gAppWindowAttr = kwAppWindow;    /* Main window attributes. */
  67.  
  68. short        gMinVersion    = kMinVersion;    /* Minimum document version app can support. */
  69. short        gMaxVersion    = kMaxVersion;    /* Maximum document version app can support. */
  70.                                             /* More informing DTS.Lib..framework. */
  71.  
  72. extern short        gPrintPage;                /* Non-zero means we are printing. */
  73.                                             /* DTS.Lib..framework global. */
  74.  
  75. extern RgnHandle    gCursorRgn;                /* We handle cursors here, so we need */
  76. extern CursPtr        gCursorPtr;                /* to know about these things. */
  77.                                             /* Above are DTS.Lib..framework globals. */
  78.  
  79. /* Currently Wannabe doesn't ever change the cursor, so we don't actually need
  80. ** these referenced here.  However, since Wannabe is supposed to be an application
  81. ** in progress, it is very likely that you will need to reference these as your
  82. ** project develops.  See DTS.Chat and DTS.Draw for examples of setting the cursor. */
  83.  
  84. /* Some cursors are pointer-based, and some cursors are resource-based.
  85. ** If a cursor is resource-based, it needs to be loaded and made to not move,
  86. ** and then gCursorPtr can be set to point to it.  This makes all cursors
  87. ** pointer-based.  Also, gCursorPtr is used by DTS.Lib..framework to
  88. ** determine if there is a current cursor.  If gCursorPtr is nil, then
  89. ** there is no current cursor, and the cursor has to be recalculated, no
  90. ** matter where the mouse is.  If gCursorPtr is not nil, then if the
  91. ** mouse position is within the cursor region gCursorRgn, the cursor is
  92. ** correct, and no recalculation is necessary.  If it is outside this region,
  93. ** then it is recalculated.  What does this all mean?  It means that if you
  94. ** want to guarantee that the cursor is recalculated next time DoWindowCursor()
  95. ** is called, set gCursorPtr to nil.
  96. **
  97. ** If you have a cursor resource, you need to:
  98. ** 1) Load the resource.
  99. ** 2) Make a fixed copy of it.
  100. ** 3) Set the cursor to it.
  101. ** 4) Set gCursorPtr to point to the fixed copy.
  102. **
  103. ** There is a function that does almost all of this, called DoSetResCursor().
  104. ** It does all but set gCursorPtr to it.  (It actually sets gCursorPtr to nil.)
  105. ** It does return a pointer to the permanent copy, so typically what you will
  106. ** want to do is the following:
  107. **     gCursorPtr = DoSetResCursor(theCursorID);
  108. **
  109. ** So why set gCursorPtr to nil as the default action?  This allows you to
  110. ** set a temporary cursor, which will be replaced when DoWindowCursor() is
  111. ** called next, or it allows you to set a cursor that maps to the cursor
  112. ** region gCursorRgn (by setting gCursorPtr to the return result). */
  113.  
  114.  
  115.  
  116. /*****************************************************************************/
  117. /*****************************************************************************/
  118.  
  119.  
  120.  
  121. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  122.  
  123. /* Calculate application specific frame area (Called by DoCalcFrameRgn).
  124. ** You are passed an empty region.  You are supposed to add any custom frame
  125. ** parts that this document uses.  Typically there are no frame portions, as
  126. ** they are accounted for in other ways.  The scrollbars and grow icon will
  127. ** automatically be contributed to the calculation of the frame region.
  128. ** If you use sidebars, these are also added in automatically.  This is only
  129. ** used if the frame region is more complicated than can automatically be
  130. ** handled.  So, almost always, you will simply leave the region empty. */
  131.  
  132. #pragma segment TheDoc
  133. void    CalcFrameRgn(FileRecHndl frHndl, WindowPtr window, RgnHandle rgn)
  134. {
  135. #pragma unused (frHndl, window, rgn)
  136. }
  137.  
  138.  
  139.  
  140. /*****************************************************************************/
  141.  
  142.  
  143.  
  144. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  145.  
  146. /* This is called (by DoContentClick()) when a mouse-down event occurs in the content of
  147. ** a window.  Other applications might want to call FindControl, TEClick, etc., to
  148. ** further process the click. */
  149.  
  150. #pragma segment TheDoc
  151. void    ContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  152. {
  153. #pragma unused (firstClick)
  154.  
  155.     ControlHandle    ctl;
  156.     short            action, cnum;
  157.  
  158.     cnum = IsCtlEvent(window, event, &ctl, &action);
  159.         /* That was easy.  Scrolling was just handled.  Other stuff would be handled
  160.         ** by IsCtlEvent, if we had other stuff to do.  We don't have any other
  161.         ** controls in the content besides the document scrollbars. */
  162.  
  163.     return;
  164. }
  165.  
  166.  
  167.  
  168. /*****************************************************************************/
  169.  
  170.  
  171.  
  172. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  173.  
  174. /* DoKeyDown() is first called by the application.  Then if the key isn't a menu
  175. ** key, DoKeyDown() calls this code.  Here are the rules for this function:
  176. **
  177. ** 1) If you handle the key, return(true).  This completes the key handling.
  178. ** 2) If you don't handle the key, you return false.  However, there are two
  179. **    situations for not handling the key:
  180. **      a) You want someone else to.
  181. **      b) You want nobody else to look at the key.
  182. **    This is what the boolean passThrough is for.  If you wish the next window
  183. **    to have a look at the key, set the boolean passThrough to true.  passThrough
  184. **    is already initialized to false, which is the common case, so you only have
  185. **    to worry about setting it true.
  186. **
  187. ** If you have a window that never processes keys and always passes them through,
  188. ** just set the contentKeyProc to nil.  This will indicate to the application
  189. ** framework that all keys should be passed through this window.  DTS.Draw has
  190. ** such a window.  Its palette window doesn't accept keys.  They are passed through
  191. ** to document windows. */
  192.  
  193. #pragma segment TheDoc
  194. Boolean    ContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  195. {
  196. #pragma unused (passThrough)
  197.  
  198.     short    cnum;
  199.  
  200.     cnum = IsCtlEvent(window, event, nil, nil);
  201.         /* See DTS.Draw for an example of what you might do here. */
  202.  
  203.     return(true);
  204. }
  205.  
  206.  
  207.  
  208. /*****************************************************************************/
  209.  
  210.  
  211.  
  212. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  213.  
  214. /* Draw application specific content (Called by DoDrawFrame).
  215. **
  216. ** If your application has any custom frame areas, or if it uses sidebars,
  217. ** this is the function that you would put the frame drawing code.  The
  218. ** document scrollbars and grow icon drawing is handled by DTS.framework.
  219. ** Just do the sidebar and custom areas here. */
  220.  
  221. #pragma segment TheDoc
  222. void    DrawFrame(FileRecHndl frHndl, WindowPtr window, Boolean activate)
  223. {
  224.     MoveTo(0, (*frHndl)->fileState.topSidebar - 1);
  225.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, (*frHndl)->fileState.topSidebar - 1);
  226.     LineTo((*frHndl)->fileState.leftSidebar - 1 - 16384, 16383);
  227.  
  228.     BeginFrame(window);
  229.     DoDrawControls(window, activate);
  230.     EndFrame(window);
  231. }
  232.  
  233.  
  234.  
  235. /*****************************************************************************/
  236.  
  237.  
  238.  
  239. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  240.  
  241. /* Frees up any application-specific memory in the document.  This is called by
  242. ** DoFreeDocument, which is called by DisposeDocument().  The application would
  243. ** call DisposeDocument(), not DoFreeDocument() or FreeDocument() directly.
  244. **
  245. ** The document may have a bunch of handles off the main handle of the document.
  246. ** This is where they are freed.  DisposeDocument calls this prior to releasing
  247. ** the ram for the main handle of the document, so release everything else
  248. ** here, or you will have a memory leak.
  249. **
  250. ** NOTE:  Calling DefaultFreeDocument() frees up all memory used by a
  251. ** hierarchical document (see TreeObj package). */
  252.  
  253. #pragma segment TheDoc
  254. OSErr    FreeDocument(FileRecHndl frHndl)
  255. {
  256.     return(DefaultFreeDocument(frHndl));
  257. }
  258.  
  259.  
  260.  
  261. /*****************************************************************************/
  262.  
  263.  
  264.  
  265. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  266.  
  267. /* Any additional window disposal tasks can be handled here. */
  268.  
  269. #pragma segment TheDoc
  270. OSErr    FreeWindow(FileRecHndl frHndl, WindowPtr window)
  271. {
  272. #pragma unused (frHndl, window)
  273.  
  274.     return(noErr);
  275. }
  276.  
  277.  
  278.  
  279. /*****************************************************************************/
  280.  
  281.  
  282.  
  283. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  284.  
  285. /* Image the document into the current port.
  286. **
  287. ** The only thing tricky about this function is that it needs to key off of
  288. ** the global variable gPrintPage.  gPrintPage is the current page that is
  289. ** being printed.  If gPrintPage is 0, then you are drawing to the window.
  290. **
  291. ** For when printing:
  292. **
  293. ** If gPrintPage is non-0, that is the page to be printed.  If after imaging
  294. ** the page there are no more pages, you should set gPrintPage to 0.  This
  295. ** indicates to the print loop that the end of the document has been reached.
  296. ** Even if the user indicated in the job dialog to print more pages, setting
  297. ** gPrintPage to 0 states that the last page has been printed.  This is necessary
  298. ** because the print loop can't know when printing is done.  The imaging procedure
  299. ** is the logical one to state when everything has been imaged. */
  300.  
  301. #pragma segment TheDoc
  302. OSErr    ImageDocument(FileRecHndl frHndl)
  303. {
  304. #pragma unused (frHndl)
  305.  
  306.     WindowPtr    curPort;
  307.  
  308.     GetPort(&curPort);
  309.     if (!gPrintPage) {                                    /* If not printing... */
  310.         DoDrawControls(curPort, false);                    /* Draw the content controls. */
  311.     }
  312.     gPrintPage = 0;
  313.  
  314.     return(noErr);
  315. }
  316.  
  317.  
  318.  
  319. /*****************************************************************************/
  320.  
  321.  
  322.  
  323. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  324.  
  325. /* This function does the remaining window initialization.
  326. **
  327. ** There may be additional content initialization for the window.  At this point,
  328. ** you have a window, but it is currently invisible.  If you return noErr, then
  329. ** the window will be set to the state indicated for that window.  Why this function?
  330. ** You may wish to add controls to the content of the window.  You may have a
  331. ** TextEdit record in the content.  All of these sort of things can't be created
  332. ** until there is a window to contain them.  First a document is read in, and then
  333. ** if the document creation succeeds, a window is created for that document.
  334. ** At this point we have a document, and we are on our way to having a window.
  335. ** All that remains is any additional content initialization.  Do it, return
  336. ** noErr, and everybody's happy.  If something goes wrong here, return the error,
  337. ** and the incomplete window will be disposed of. */
  338.  
  339. #pragma segment TheDoc
  340. OSErr    InitContent(FileRecHndl frHndl, WindowPtr window)
  341. {
  342.     OSErr    err;
  343.  
  344.     err = AddControlSet(window, (*frHndl)->fileState.sfType, kwStandardVis, 0, 0, nil);
  345.     return(err);
  346. }
  347.  
  348.  
  349.  
  350. /*****************************************************************************/
  351.  
  352.  
  353.  
  354. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  355.  
  356. /* The code below assumes that you are using the hierarchical document package.
  357. ** If you are, the entire hierarchical document is read in with just these two
  358. ** calls.  If you don't use it, you are on your own.  See DTS.Chat for an
  359. ** example of an application that uses the DTS.framework without the hierarchical
  360. ** document package. */
  361.  
  362. #pragma segment TheDoc
  363. OSErr    ReadDocument(FileRecHndl frHndl)
  364. {
  365.     OSErr    err;
  366.  
  367.     err = DefaultReadDocument(frHndl);
  368.     if (!err)
  369.         DefaultReadDocumentFixup(frHndl);
  370.  
  371.     return(err);
  372. }
  373.  
  374.  
  375.  
  376. /*****************************************************************************/
  377.  
  378.  
  379.  
  380. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  381.  
  382. /* Resize application specific content (Called by ResizeWindow).
  383. **
  384. ** This gets called when a user does a zoom or window resizing operation.
  385. ** It is possible that things in the content need to be resized in conjunction
  386. ** with the resizing of the window. */
  387.  
  388. #pragma segment TheDoc
  389. void    ResizeContent(WindowPtr window, short oldh, short oldv)
  390. {
  391. #pragma unused (window, oldh, oldv)
  392.  
  393.     /* See DTS.Chat for a sample usage of this function. */
  394. }
  395.  
  396.  
  397.  
  398. /*****************************************************************************/
  399.  
  400.  
  401.  
  402. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  403.  
  404. /* Scroll application specific frame (Called by DoScrollFrame).
  405. **
  406. ** Some applications may need to scroll the "frame" of the document along
  407. ** with the document contents.  This is common for applications with rulers,
  408. ** or other similar sidebar items. */
  409.  
  410. #pragma segment TheDoc
  411. void    ScrollFrame(FileRecHndl frHndl, WindowPtr window, long dh, long dv)
  412. {
  413. #pragma unused (frHndl, window, dh, dv)
  414. }
  415.  
  416.  
  417.  
  418. /*****************************************************************************/
  419.  
  420.  
  421.  
  422. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  423.  
  424. /* Since the hierarchical document package isn't used by DTS.Chat,
  425. ** this function actually never gets called. */
  426.  
  427. #pragma segment TheDoc
  428. void    UndoFixup(FileRecHndl frHndl, Point contOrg, Boolean afterUndo)
  429. {
  430. #pragma unused (frHndl, contOrg, afterUndo)
  431.  
  432.     /* See DTS.Draw for an example of what you might do here. */
  433. }
  434.  
  435.  
  436.  
  437. /*****************************************************************************/
  438.  
  439.  
  440.  
  441. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  442.  
  443. /* This function is where you adjust the cursor to reflect the location in the
  444. ** document or window.  You have the additional input of gCursorRgn to deal
  445. ** with.  The way that the cursor handling works is as follows:
  446. ** 1) The application calls DoWindowCursor().
  447. ** 2) DoWindowCursor() works its way through the windows/documents, front to back.
  448. **    It looks at the document's windowCursorProc and checks to see if the document
  449. **    has one.  If the document doesn't have one, then it assumes that that window
  450. **    always wants an arrow.  If the cursor is over that window, the cursor is set
  451. **    to an arrow, and we're done.  If the cursor isn't over the window, then the next
  452. **    window is tried.  If all documents don't have a windowCursorProc, then the cursor
  453. **    is set to an arrow (for the non-document area of the screen).
  454. ** 3) If a document has a windowCursorProc, then the proc is called.  The proc's
  455. **    job is as follows:
  456. **    a) If the cursor is over a position that is determined by the window, then
  457. **       the proc removes other areas from gCursorRgn.  Note that it should not
  458. **       simply set the area to what it "thinks" is the correct area.  This window
  459. **       may not be the front-most.  Other windows will have already been subtracted
  460. **       from gCursorRgn.  The resultant gCursorRgn is the correct cursor area,
  461. **       and should be passed to WaitNextEvent calls in the application (already the case
  462. **       in EventLoop.c).  Also, the cursor should be set to the correct cursor, of course.
  463. **       You should also return true, as the cursor has been determined.
  464. **    b) If the cursor is not over a position for this window, then you should
  465. **       return.  You will either pass back true or false.  If you don't wish
  466. **       windows behind this window to have a shot at cursor determination, then
  467. **       return true.  This states that the cursor is "determined".  It is, in the
  468. **       sense that no further determination will occur.  If you return false, then
  469. **       other windows get a shot at determining the cursor.
  470. **
  471. ** Setting the cursor to the correct cursor isn't as easy as you would expect.
  472. ** DTS.Lib..framework uses the global gCursorPtr as the reference to the cursor.  This is
  473. ** fine if the cursor is pointer-based, but if the cursor is resource-based, it is a bit
  474. ** more of a problem.  What you will need to do is to call DoSetResCursor() to make the
  475. ** resource cursor pointer-based.  DoSetResCursor() will set gCursorPtr to nil, and it
  476. ** also returns the pointer to the permanent copy of the cursor resource.  Just set gCursorPtr
  477. ** to the return result of DoSetResCursor(), and you will be set. */
  478.  
  479. #pragma segment TheDoc
  480. Boolean    WindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  481. {
  482. #pragma unused (frHndl, window, globalPt)
  483.  
  484.     /* For examples of applications that have non-arrow cursor regions,
  485.     ** see DTS.Chat and DTS.Draw. */
  486.  
  487.     DoSetCursor(&qd.arrow);
  488.     return(true);
  489. }
  490.  
  491.  
  492.  
  493. /*****************************************************************************/
  494.  
  495.  
  496.  
  497. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  498.  
  499. /* After the DTS.Lib framework disposes of a window, it calls here.  This is
  500. ** to give the application a chance to do any additional tasks related to
  501. ** a window closing.  DTS.Chat doesn't have anything else extra to do. */
  502.  
  503. #pragma segment TheDoc
  504. void    WindowGoneFixup(WindowPtr window)
  505. {
  506. #pragma unused (window)
  507. }
  508.  
  509.  
  510.  
  511. /*****************************************************************************/
  512.  
  513.  
  514.  
  515. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  516.  
  517. /* The reverse function of ReadDocument. */
  518.  
  519. #pragma segment TheDoc
  520. OSErr    WriteDocument(FileRecHndl frHndl)
  521. {
  522.     return(DefaultWriteDocument(frHndl));
  523. }
  524.  
  525.  
  526.  
  527. /*****************************************************************************/
  528.  
  529.  
  530.  
  531. /* •• You don't call this.  DTS.Lib..framework does at open-application time. •• */
  532.  
  533. #pragma segment TheDoc
  534. OSErr    DoOpenApplication(void)
  535. {
  536.     return(noErr);
  537. }
  538.  
  539.  
  540.  
  541. /*****************************************************************************/
  542. /*****************************************************************************/
  543. /*****************************************************************************/
  544.  
  545.  
  546.  
  547. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  548.  
  549. #pragma segment TheDoc
  550. Boolean    AdjustMenuItems(WindowPtr window, short menuID)
  551. {
  552.     Boolean        redrawMenuBar;
  553.     MenuHandle    menu;
  554.  
  555.     redrawMenuBar = false;
  556.  
  557.     switch (menuID) {
  558.         case mFile:
  559.             redrawMenuBar = DoAdjustFileMenu(window);
  560.             break;
  561.         case mEdit:
  562.             redrawMenuBar = DoAdjustEditMenu(window);
  563.             break;
  564.         default:
  565.             if (menu = GetMHandle(menuID))
  566.                 (*menu)->enableFlags |= 0xFFFFFFFEL;
  567.             break;
  568.     }
  569.  
  570.     return(redrawMenuBar);
  571. }
  572.  
  573.  
  574.  
  575. /*****************************************************************************/
  576.  
  577.  
  578.  
  579. /* •• You don't call this.  DTS.Lib..framework does for appropriate document type(s). •• */
  580.  
  581. #pragma segment TheDoc
  582. Boolean    DoMenuItem(WindowPtr window, short menuID, short menuItem)
  583. {
  584. #pragma unused (window)
  585.  
  586.     return(DoMenuCommand(menuID, menuItem));
  587. }
  588.  
  589.  
  590.  
  591.